package com.zcp.recycleviewmoreitem;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RecycleAda.Call2 {

    private RecyclerView recycle;
    public static DisplayImageOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        options = new DisplayImageOptions.Builder().build();
        recycle = (RecyclerView) findViewById(R.id.recycle);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recycle.setLayoutManager(manager);

        getData();
    }

    public void getData() {
        //创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
        //创建一个Request
        final Request request = new Request.Builder()
                .url("http://v.juhe.cn/toutiao/index?type=top&key=24afc02b132f625cb771f7c722caca96")
                .build();
        //new call
        Call call = mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
            }

            @Override
            public void onResponse(final Response response) throws IOException {
                final String htmlStr = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        Bean bean = gson.fromJson(htmlStr, Bean.class);
                        List<Bean.ResultBean.DataBean> list = bean.result.data;

                        //Log.d("zzz",bean.toString());
                        RecycleAda ada = new RecycleAda(MainActivity.this, list);
                        recycle.setAdapter(ada);
                        ada.setListener(MainActivity.this);

                    }
                });

            }
        });
    }

    @Override
    public void setUrl(String url) {
        Toast.makeText(MainActivity.this, "路径" + url, Toast.LENGTH_SHORT).show();
    }
}
